/*
 * ImageState.java
 *
 * Created on January 12, 2008, 3:22 PM
 */

package picto.core;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author davedes
 */
public class ImageState {
    
    protected BufferedImage image;
    protected Graphics2D g2d;
    
    public static final int DEFAULT_RGB = BufferedImage.TYPE_INT_RGB;
    public static final int DEFAULT_ARGB = BufferedImage.TYPE_INT_ARGB;
    public static final int DEFAULT_ARGB_PRE = BufferedImage.TYPE_INT_ARGB_PRE;
    
    /** Creates a new instance of ImageState */
    public ImageState(int width, int height, int type) {
        this(new BufferedImage(width, height, type));
    }
    
    public ImageState(int width, int height) {
        this(width, height, DEFAULT_ARGB);
    }
    
    public ImageState(BufferedImage image) {
        this.image = image;
        this.g2d = image.createGraphics();
    }
    
    public ImageState() {
    }

    public int getWidth() {
        return getImage().getWidth();
    }
    
    public int getHeight() {
        return getImage().getHeight();
    }
    
    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
        if (image!=null)
            this.g2d = image.createGraphics();
    }
    
    public void dispose() {
        if (g2d!=null) {
            g2d.dispose();
            g2d = null;
        }
    }

    public Graphics2D getGraphics() {
        if (g2d==null && image!=null)
            g2d = image.createGraphics();
        return g2d;
    }
}
